package br.com.aquinoevasconcelos.servlets.administrador;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.aquinoevasconcelos.fachada.Fachada;
import br.com.aquinoevasconcelos.modelos.Endereco;
import br.com.aquinoevasconcelos.user.UsuarioAdm;

/**
 * Servlet implementation class InserirAdministrador
 */
@WebServlet("/InserirAdministrador")
public class InserirAdministrador extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

    public InserirAdministrador() {
        super();
    }
    
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	
    	Fachada fachada = Fachada.getInstancia();
    	UsuarioAdm adm = new UsuarioAdm();
    	Endereco endereco = new Endereco();
    	
    	adm.setNome(request.getParameter("nome"));
    	adm.setLogin(request.getParameter("login"));
    	adm.setSenha(request.getParameter("senha"));
    	adm.setCnpj(request.getParameter("cnpj"));
    	adm.setEmail(request.getParameter("email"));
    	adm.setCelular1(request.getParameter("celular1"));
    	adm.setCelular2(request.getParameter("celular2"));
    	adm.setTelefone(request.getParameter("telefone"));
    	
    	endereco.setBairro(request.getParameter("bairro"));
    	endereco.setCep(request.getParameter("cep"));
    	endereco.setCidade(request.getParameter("cidade"));
    	endereco.setComplemento(request.getParameter("complemento"));
    	endereco.setEstado(request.getParameter("estado"));
    	endereco.setRua(request.getParameter("rua"));
    	
    	adm.setEndereco(endereco);
    	
    	String senhaMestre = request.getParameter("senhaMestre");
    	
		try {

			if(fachada.validarSenhaMestre(senhaMestre) == true){
				
				fachada.inserirADM(adm);

				response.sendRedirect("EncaminharPainelAdm?msg=sucesso");	
			}
			else {
				
				response.sendRedirect("./admin/novo_administrador.jsp");
			}


		} catch (Exception e) {
			RequestDispatcher rd = request.getRequestDispatcher("pagina_erro/erro.jsp");
			rd.forward(request, response);
			e.printStackTrace();
		}

    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		service(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		service(request, response);
		
	}

}
